package Visao;

import Modelo.Hotel;
import Modelo.Quarto;
import RMI.ServicosCliente;
import java.rmi.RemoteException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Kassio Kiarelly <kassiokiarelly@gmail.com>
 */
public class FormQuarto extends javax.swing.JDialog {

    private Hotel hotel;

    public FormQuarto() {
        initComponents();
        setLocationRelativeTo(null);
    }

    private void popularHoteis() throws RemoteException {

        cmbHoteis.removeAllItems();
        for (Hotel h : ServicosCliente.getInstance().getHoteis()) {
            cmbHoteis.addItem(h);
        }
    }

    private void popularQuartos(Hotel hotel) {
        List<Quarto> quartos = hotel.getQuartos();
        DefaultTableModel modelo = (DefaultTableModel) grid.getModel();
        modelo.setNumRows(0);
        if (quartos.size() > 0) {
            for (Quarto quarto : quartos) {
                modelo.addRow(new Object[]{quarto.getNumero()});
            }
        }

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cmbHoteis = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        grid = new javax.swing.JTable();
        btAdd = new javax.swing.JButton();
        salvar = new javax.swing.JButton();

        setTitle("Cadastro de Quartos");
        setModal(true);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("Selecione o hotel que você deseja adicionar o quarto");

        cmbHoteis.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbHoteisItemStateChanged(evt);
            }
        });

        grid.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Quarto"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(grid);

        btAdd.setText("Adicionar quarto");
        btAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddActionPerformed(evt);
            }
        });

        salvar.setText("Salvar");
        salvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(cmbHoteis, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btAdd)
                            .addGap(18, 18, 18)
                            .addComponent(salvar))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbHoteis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(salvar)
                    .addComponent(btAdd))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {
            popularHoteis();
        } catch (RemoteException ex) {
            Logger.getLogger(FormQuarto.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formWindowOpened

    private void cmbHoteisItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbHoteisItemStateChanged
        popularQuartos((Hotel) cmbHoteis.getSelectedItem());
    }//GEN-LAST:event_cmbHoteisItemStateChanged

    private void btAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionPerformed
        Hotel h = (Hotel) cmbHoteis.getSelectedItem();
        int proxQuarto = h.getQuartos().size() + 1;
        h.addQuarto(new Quarto(proxQuarto, h, 210));
        this.hotel = h; 
        popularQuartos(this.hotel);
    }//GEN-LAST:event_btAddActionPerformed

    private void salvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarActionPerformed
        // TODO add your handling code here:
        try {
            ServicosCliente.getInstance().editHotel(this.hotel);
            JOptionPane.showMessageDialog(null, "Quartos adicionados com sucesso.");
        } catch (Exception e) {}
    }//GEN-LAST:event_salvarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdd;
    private javax.swing.JComboBox cmbHoteis;
    private javax.swing.JTable grid;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton salvar;
    // End of variables declaration//GEN-END:variables
}
